package com.webservice.xmloper.activeinfo;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class CpuConvertor implements Converter{

	public void marshal(Object source, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		CpuObject cpuObject = null;
		if(source != null ){
			cpuObject = (CpuObject) source;
		}
		if(cpuObject != null && cpuObject.getCpu() != null){
			writer.setValue(cpuObject.getCpu().toString());
		}else{
			writer.setValue("0.00");
		}
	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		CpuObject cpuObject = new CpuObject();
		try{
			Float cpu = new Float(reader.getValue());
			cpuObject.setCpu(cpu);
		}catch(Exception e){
			e.printStackTrace();
			cpuObject.setCpu(new Float(0.00));
		}
		return cpuObject;
	}

	public boolean canConvert(Class type) {
		return type.equals(CpuObject.class);
	}
}
